﻿///***************************************************************
/// 시  스  템 : Updms.Crd
/// 단위시스템 : CRD
/// 계      층 : Presentation Layer
/// 프로그램명 : Crd2012r
/// 파  일  명 : Crd2012r.aspx.cs
/// 설      명 : 결제총액챠트(월별)
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-08-06 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Crd.Exe;
using Updms.Common.BaseClass;


namespace Updms.Crd
{
	public partial class Crd2012r : PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Crd2012r";
		public static readonly string gs_path_info = "/updms/Crd/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Com com = new CommCode.Com(LANG_SET);
		CommCode.Crd crd = new CommCode.Crd(LANG_SET);

		//검색
		protected string kw1 = "";	//검색시작년월
		protected string kw2 = "";	//검색종료년월
		protected string kw3 = "";	//미반영
		protected string kw4 = "";	//미반영
		protected string kw5 = "";	//챠트넓이

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetCardInfo();
			SetYearMonth();
			SetAccount();
			SetImportantAccount();
			SetChartWidth();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion	

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";
			
			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_from_year.SelectedValue + "-" + ddl_search_from_month.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_from_year.SelectedValue = kw1.Substring(0, 4);
						ddl_search_from_month.SelectedValue = kw1.Substring(5, 2);
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = ddl_search_to_year.SelectedValue + "-" + ddl_search_to_month.SelectedValue;
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						ddl_search_to_year.SelectedValue = kw2.Substring(0, 4);
						ddl_search_to_month.SelectedValue = kw2.Substring(5, 2);
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw5"]))	// url에 값이 없으면
					{
						kw5 = ddl_search_chart_width.SelectedValue;
					}
					else
					{
						kw5 = Server.UrlDecode(Request.QueryString["kw5"]);
						ddl_search_chart_width.SelectedValue = kw5;
					}
					break;
				case "SEARCH":
					kw1 = ddl_search_from_year.SelectedValue + "-" + ddl_search_from_month.SelectedValue;
					kw2 = ddl_search_to_year.SelectedValue + "-" + ddl_search_to_month.SelectedValue;
					kw5 = ddl_search_chart_width.SelectedValue;
					break;
			}

			kw3 = cb_support_yn.Checked ? "CRD" : "";
			kw4 = cb_support_yn.Checked ? "AG" : "";
		}

		#endregion

		#region 마스터 Method (SetCardInfo, SetYearMonth, SetAccount, SetImportantAccount, SetChartWidth)

		//카드정보
		private void SetCardInfo()
		{
			DataSet lo_ds = new DataSet();
			Crd1010m_NT _Crd1010m_NT = new Crd1010m_NT();

			lo_ds.Merge(_Crd1010m_NT.GetCardInfoList("A"));

			cbl_search_card_cd.Items.Clear();
			cbl_search_card_cd.DataSource = lo_ds;
			cbl_search_card_cd.DataMember = "UPDMS_CRD_INFO";
			cbl_search_card_cd.DataTextField = "Card_Nm_Color";
			cbl_search_card_cd.DataValueField = "Card_Cd";
			cbl_search_card_cd.DataBind();
			
			if (cbl_search_card_cd.Items.Count > 0)
			{
				cbl_search_card_cd.Items[0].Selected = true;
			}
		}

		// 검색조건의 년월설정
		private void SetYearMonth()
		{
			ddl_search_from_year.Items.Clear();
			ddl_search_to_year.Items.Clear();

			// 현재년도 기준으로 ±2년 -> SEARCH_Y_MIN년부터 현재+1년
			int li_f_year = Convert.ToInt32(SEARCH_Y_MIN);
			int li_t_year = Convert.ToInt32(DateUtil.GetYearEx(1, "yyyy"));
			for (int i = li_f_year; i <= li_t_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_from_year.Items.Add(new ListItem(ls_year, ls_year));
				ddl_search_to_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			SetYearMonth(-12);
		}

		// 검색조건의 년월설정 - 기간지정
		private void SetYearMonth(int li_term)
		{
			ddl_search_from_year.SelectedValue = DateUtil.GetMonthEx(li_term, "yyyy"); //x개월전년도
			ddl_search_from_month.SelectedValue = DateUtil.GetMonthEx(li_term, "MM"); //x개월전월
			ddl_search_to_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 현재년도
			ddl_search_to_month.SelectedValue = DateUtil.GetMonthEx(0, "MM"); // 현재월
		}

		//계정
		private void SetAccount()
		{
			crd.SetAccount(cbl_search_account);
		}
		
		//주요계정만 미리 선택
		private void SetImportantAccount()
		{
			DataSet lo_ds = new DataSet();
			lo_ds.Merge(global.GetCommCdAttr("CRD", "AE", LANG_SET, 1));
			
			for (int i=0; i< cbl_search_account.Items.Count; i++)
			{
				for (int j = 0; j < DataSetUtil.GetRowCount(lo_ds, "UPDMS_COM_CD"); j++)
				{
					string ls_value = DataSetUtil.GetFldStr(lo_ds, "UPDMS_COM_CD", j, "Value");
					if (cbl_search_account.Items[i].Value == ls_value)
					{
						cbl_search_account.Items[i].Selected = true;
					}
				}
			}
		}

		//챠트넓이
		private void SetChartWidth()
		{
			com.SetChartWidth(ddl_search_chart_width);

			ddl_search_chart_width.SelectedValue = CHART_RESOLUTION;
		}

		#endregion

		#region 챠트 (Draw..., SearchProcess)

		#region 결제총액챠트, 결제차액챠트

		//결제총액챠트, 결제차액챠트
		private void DrawMonthlyTotalAmtList()
		{
			int li_line_cnt = 0; // 그래프 라인수
			
			//초기화
			chart_mline_total.Data.Clear();
			chart_mline_diff.Data.Clear();

			//시리즈 갯수
			int li_series = WebControlUtil.GetCheckedItemsCount(cbl_search_card_cd);
			chart_mline_total.Data.Series = li_series;
			chart_mline_diff.Data.Series = li_series;

			//넓이
			chart_mline_total.Width = Convert.ToInt32(kw5);
			chart_mline_diff.Width = Convert.ToInt32(kw5);

			for (int i = 0; i < cbl_search_card_cd.Items.Count; i++)
			{
				if (cbl_search_card_cd.Items[i].Selected == true)
				{
					DataSet lo_ds = new DataSet();
					DataPack lo_dp = new DataPack();

					lo_dp.AddProperty("ls_card_cd", typeof(string), cbl_search_card_cd.Items[i].Value);
					lo_dp.AddProperty("ls_from_dt", typeof(string), kw1);
					lo_dp.AddProperty("ls_to_dt", typeof(string), kw2);
					lo_dp.AddProperty("ls_exc_sys_id", typeof(string), kw3);
					lo_dp.AddProperty("ls_exc_prefix", typeof(string), kw4);
					
					using (Crd2012r_NT _Crd2012r_NT = new Crd2012r_NT())
					{
						lo_ds.Merge(_Crd2012r_NT.GetMonthlyTotalAmtList(lo_dp));
					}
					
					int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CRD_REPORT");

					for (int j = 0; j < li_count; j++)
					{
						//수동으로 데이터 바인딩
						chart_mline_total.Data[li_line_cnt, j] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_REPORT", j, "Total_Amt");
						chart_mline_diff.Data[li_line_cnt, j] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_REPORT", j, "Diff_Amt");

						//X축 라벨링
						string ls_label = "'" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_REPORT", j, "Base_Dt").Replace("-", ".").Substring(2, 5);
						chart_mline_total.AxisX.Labels[j] = ls_label;
						chart_mline_diff.AxisX.Labels[j] = ls_label;

						//0은 안보여준다
						chart_mline_total.Points[li_line_cnt, j].PointLabels.Visible = (chart_mline_total.Data[li_line_cnt, j] > 0);
						chart_mline_diff.Points[li_line_cnt, j].PointLabels.Visible = (chart_mline_diff.Data[li_line_cnt, j] > 0);
					}

					//시리즈
					chart_mline_total.Series[li_line_cnt].Text = StringUtil.DeleteHtmlTag(cbl_search_card_cd.Items[i].Text);
					chart_mline_diff.Series[li_line_cnt].Text = StringUtil.DeleteHtmlTag(cbl_search_card_cd.Items[i].Text);
					li_line_cnt = li_line_cnt + 1;
				}//if
			}//for

			//CustomGridLine
			chart_mline_total.AxisY.CustomGridLines.Clear();
			ChartFX.WebForms.CustomGridLine cgl1 = new ChartFX.WebForms.CustomGridLine(1000000, "");
			cgl1.Color = System.Drawing.Color.Red;
			cgl1.Width = 1;
			//cgl1.ExtraStyle = ChartFX.WebForms.CustomGridLineStyles.BackOnly;	//챠트뒤로
			chart_mline_total.AxisY.CustomGridLines.Add(cgl1);
		}
		
		#endregion

		#region 계정별총액챠트, 계정별분포도

		//계정별총액챠트
		private void DrawMonthlyTotalAmtListByAccount()
		{
			int li_line_cnt = 0; // 그래프 라인수
			string ls_card_cd = "";
			DataPack lo_dp = new DataPack();

			//카드코드 (IN절에 들어갈 카드코드 문자열을 생성한다.) CC01X,CC02X,CC03X
			for (int i = 0; i < cbl_search_card_cd.Items.Count; i++)
			{
				if (cbl_search_card_cd.Items[i].Selected == true)
				{
					ls_card_cd += cbl_search_card_cd.Items[i].Value + ",";
				}
			}
			ls_card_cd = ls_card_cd.Substring(0, ls_card_cd.LastIndexOf(","));

			//초기화
			chart_mline_account.Data.Clear();
			chart_mline_account.Data.Series = WebControlUtil.GetCheckedItemsCount(cbl_search_card_cd);

			//넓이
			chart_mline_account.Width = Convert.ToInt32(kw5);

			lo_dp.AddProperty("ls_card_cd", typeof(string), ls_card_cd);
			lo_dp.AddProperty("ls_base_st_dt", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_ed_dt", typeof(string), kw2);
			lo_dp.AddProperty("ls_account", typeof(string));	//선언만

			for (int i = 0; i < cbl_search_account.Items.Count; i++)
			{
				if (cbl_search_account.Items[i].Selected == true)
				{
					DataSet lo_ds = new DataSet();

					lo_dp["ls_account"] = cbl_search_account.Items[i].Value;
					
					using (Crd2012r_NT _Crd2012r_NT = new Crd2012r_NT())
					{
						lo_ds.Merge(_Crd2012r_NT.GetMonthlyTotalAmtListByAccount(lo_dp));
					}
					
					int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CRD_REPORT");

					for (int j = 0; j < li_count; j++)
					{
						//수동으로 데이터 바인딩
						chart_mline_account.Data[li_line_cnt, j] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_REPORT", j, "Total_Amt");

						//X축 라벨링
						chart_mline_account.AxisX.Labels[j]
							= "'" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_REPORT", j, "Base_Dt").Replace("-", ".").Substring(2, 5);

						//0은 안보여준다
						chart_mline_account.Points[li_line_cnt, j].PointLabels.Visible = (chart_mline_account.Data[li_line_cnt, j] > 0);
					}

					//시리즈
					chart_mline_account.Series[li_line_cnt].Text = cbl_search_account.Items[i].Text;
					
					chart_mline_account.Series[li_line_cnt].MarkerShape = ChartFxUtil.markerShape(li_line_cnt);
					
					li_line_cnt = li_line_cnt + 1;
				}//if
			}//for
		}

		//계정별분포도
		private void DrawTotalAmtDistributeByAccount()
		{
			string ls_card_cd = "";
			string ls_account = "";
			
			DataPack lo_dp = new DataPack();
			DataSet lo_ds = new DataSet();

			//초기화
			chart_pie_account.Data.Clear();

			//카드코드
			for (int i = 0; i < cbl_search_card_cd.Items.Count; i++)
			{
				if (cbl_search_card_cd.Items[i].Selected == true)
				{
					ls_card_cd += cbl_search_card_cd.Items[i].Value + ",";
				}
			}
			ls_card_cd = ls_card_cd.Substring(0, ls_card_cd.LastIndexOf(","));


			//계정코드
			for (int i = 0; i < cbl_search_account.Items.Count; i++)
			{
				if (cbl_search_account.Items[i].Selected == true)
				{
					ls_account += cbl_search_account.Items[i].Value + ",";
				}
			}
			ls_account = ls_account.Substring(0, ls_account.LastIndexOf(","));

			//시리즈
			chart_pie_account.Data.Series = WebControlUtil.GetCheckedItemsCount(cbl_search_card_cd);

			//넓이
			chart_pie_account.Width = Convert.ToInt32(kw5);

			lo_dp.AddProperty("ls_card_cd", typeof(string), ls_card_cd);
			lo_dp.AddProperty("ls_base_st_dt", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_ed_dt", typeof(string), kw2);
			lo_dp.AddProperty("ls_account", typeof(string), ls_account);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Crd2012r_NT _Crd2012r_NT = new Crd2012r_NT())
			{
				lo_ds.Merge(_Crd2012r_NT.GetTotalAmtDistributeByAccount(lo_dp));
			}

			int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CRD_REPORT");

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_pie_account.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_REPORT", i, "Total_Amt");

				//X축 라벨링
				chart_pie_account.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_REPORT", i, "Account_Nm");
			}
		}
		
		#endregion

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			DrawMonthlyTotalAmtList();
			DrawMonthlyTotalAmtListByAccount();
			DrawTotalAmtDistributeByAccount();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_chart_1.Visible = false;
					table_chart_2.Visible = false;
					table_chart_3.Visible = false;
					table_chart_4.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_chart_1.Visible = true;
					table_chart_2.Visible = true;
					table_chart_3.Visible = true;
					table_chart_4.Visible = true;
					break;
			}
		}

		#endregion

		#region 버튼이벤트

		//6개월
		protected void sbtn_6_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-5);
		}

		//12개월
		protected void sbtn_12_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-12);
		}

		//전체
		protected void sbtn_all_m_Click(object sender, EventArgs e)
		{
			ddl_search_from_year.SelectedIndex = 0;
			ddl_search_from_month.SelectedIndex = 0;
			ddl_search_to_year.SelectedIndex = ddl_search_to_year.Items.Count - 1;
			ddl_search_to_month.SelectedIndex = ddl_search_to_month.Items.Count - 1;
		}

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		#endregion

	}
}
